package com.css.fxfzaqbz.modules.assess.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.BigSecondDisastersEntity;
import com.css.fxfzaqbz.modules.assess.repository.BigSecondRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author y1fan
 * @create 2022-09-19 9:59
 */
@Repository
public class BigSecondRepositoryImpl implements BigSecondRepository {

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Resource
    JdbcTemplate jdbcTemplate;
    @Override
    public BigSecondDisastersEntity getBigSecondDisastersByActiveId(String ssdRelation) {
        String sql="";
        sql="select u.* from sys_big_seconddisasters u  where  u.bs_activeid='"+ssdRelation+"' and u.bs_del_flag = '"+ YNEnum.N.toString() +"'";
        BigSecondDisastersEntity bigSecondDisastersEntity = null;
        try {
            bigSecondDisastersEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(BigSecondDisastersEntity.class));
            return bigSecondDisastersEntity;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void saveBigSecondDisasters(BigSecondDisastersEntity bigSecondDisastersEntity) {
        String sql = "insert into sys_big_seconddisasters  (bs_id,bs_activeid,bs_opinion,bs_status,bs_createtime,bs_del_flag,bs_createuser,content)values(:bsId,:bsActiveId,:bsOpinion,:bsStatus,:bsCreateTime,:bsDelFlag,:bsCreateUser,:content)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(bigSecondDisastersEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public void updateBigStatus(String bbId) {
        String sql = "update sys_big_seconddisasters set" +
                " bs_status = '"+YNEnum.Y+"' " +
                " where bs_id = '" + bbId + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void upStatus(String id) {
        String sql = "update sys_big_seconddisasters set" +
                " bs_status = '"+2+"' " +
                " where bs_activeid = '" + id + "'";
        jdbcTemplate.update(sql);
    }
}
